package com.lagou.edu.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api/email")
public class EmailController {
    @Autowired
    private JavaMailSender mailSender;


    @Value("${spring.mail.username}")
    private String from;
    @Value("${spring.mail.password}")
    private String password;


    /**
     * 发送验证码到邮箱，true成功，false失败
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/{email}/{code}")
    public boolean sendEmail(@PathVariable String email,
                             @PathVariable String code) {
        System.out.println("准备发送邮件：" + email + "；验证码：" + code);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); // 邮件发送者
        message.setTo(email); // 邮件接受者
        message.setSubject("lagou-spring-cloud-注册验证码"); // 主题
        message.setText("验证码：" + code); // 内容

        mailSender.send(message);
        return true;
    }
}
